#include<iostream>
#include<list>

using namespace std;

//给定两个以排序表L1 L2，只使用基本的操作编写计算L1 与 L2的交集

list<int> myAnd(list<int> &a, list<int> &b){
    list<int> result;
    auto p1 = a.begin();
    auto p2 = b.begin();
    while (p1!=a.end() && p2!=b.end())
    {
        if(*p1 == *p2){
            result.push_back(*p1);
            p1++;
            p2++;
        }
        else if(*p1 < *p2){
            p1++;

        }
        else{
            p2 ++;
        }
    }
    return result;

    

}

int main(int argc, char *argv[])
{
    
    
    list<int> a{1,2,3,4,5};
    list<int> b{2,4,5,8};
    auto c = myAnd(b,a);
    for(auto i = c.begin();i!=c.end();i++){
        cout << *i<< " ";

    }
    cout << endl;
    return 0;
}
